package org.gwu.seuss.server.command;

import org.gwu.seuss.communication.domain.UserTO;
import org.gwu.seuss.communication.request.ClientRequest;
import org.gwu.seuss.communication.request.ServerResponse;
import org.springframework.util.Assert;

public class CreateUserCommand extends UserCommandBase implements Command {

	public CreateUserCommand(ClientRequest req) {
		super(req);
	}

	@Override
	public ServerResponse execute() throws Exception {
		String username = req.getUsername();
		String password = req.getPassword();
		UserTO newUser = req.getUser();

		Assert.hasText(username, "Username is not provided.");
		Assert.hasText(password, "Password is not provided.");
		Assert.notNull(newUser, "New user is not provided");

		String newUsername = newUser.getUsername();
		String newPassword = newUser.getPassword();
		Assert.hasText(newUsername, "New username is not provided");
		Assert.hasText(newPassword, "New password is not provided");

		userService.authorizeAdmin(username);
		log.info("User " + username + "requested to create new user "
				+ newUsername);

		int result = userService.createUser(newUsername, newPassword);

		return getServerResponse(result, "User " + newUsername + " created.");
	}

}